/**
 * SharedPreference.java
 * com.jamin.aabill.sharepref
 *
 * Function�?TODO
 *
 *   ver     date      		author
 * ???????????????????????????????????????????????????
 *   		 2012-8-22 		wangjieming
 *
 * Copyright (c) 2012, TNT All Rights Reserved.
 */
package com.ansion.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;


/**
 * ClassName:SharedPreference
 *
 * @author wangjieming
 * @version 1.0
 * @since v1.0
 * @Date 2012-8-22
 *
 * // update to mmkv
 */
public class SharedPref implements IPreferenceStore {

	private SharedPreferences mSharedPreferences = null;
	private Editor mEditor = null;
	private volatile static SharedPref sharePref = null;

	/**
	 * register in Application
	 *
	 * @method getInstance
	 * @param ctx
	 * @return
	 * @throws
	 * @since v1.0
	 */
	public  static SharedPref getInstance(Context ctx) {
		if (sharePref == null) {
			synchronized (SharedPref.class) {
				if (sharePref == null) {
					sharePref = new SharedPref();
				}
			}
		}

		return sharePref;
	}

	public  static SharedPref getInstance() {
		return getInstance(null);
	}

	private SharedPref() {
		//todo update to mmkv
		String name = ResourceUtils.getContext().getPackageName();
		mSharedPreferences = ResourceUtils.getSharedPreferences(name);
	}

	public String getString(String key, String devValue) {
		return mSharedPreferences.getString(key, devValue);
	}

	public boolean getBoolean(String key, boolean defValue) {
		return mSharedPreferences.getBoolean(key, defValue);
	}

	public int getInt(String key, int defValue) {
		return mSharedPreferences.getInt(key, defValue);
	}

	public long getLong(String key, long defValue) {
		return mSharedPreferences.getLong(key, defValue);
	}

	public boolean contains(String key) {
		return mSharedPreferences.contains(key);
	}

	public float getFloat(String key, float defValue) {
		return mSharedPreferences.getFloat(key, defValue);
	}

	public synchronized boolean putString(String key, String value) {
		mEditor = mSharedPreferences.edit();
		mEditor.putString(key, value);
		return mEditor.commit();
	}

	public synchronized boolean putBoolean(String key, boolean value) {
		mEditor = mSharedPreferences.edit();
		mEditor.putBoolean(key, value);
		return mEditor.commit();
	}

	public synchronized boolean putFloat(String key, float value) {
		mEditor = mSharedPreferences.edit();
		mEditor.putFloat(key, value);
		return mEditor.commit();
	}

	public synchronized boolean putLong(String key, long value) {
		mEditor = mSharedPreferences.edit();
		mEditor.putLong(key, value);
		return mEditor.commit();
	}

	public synchronized boolean putInt(String key, int value) {
		mEditor = mSharedPreferences.edit();
		mEditor.putInt(key, value);
		return mEditor.commit();
	}

	public synchronized boolean removeKey(String key) {
		mEditor = mSharedPreferences.edit();
		mEditor.remove(key);
		return mEditor.commit();
	}

	public synchronized boolean clear() {
		mEditor.clear();
		return mEditor.commit();
	}

	public synchronized Editor editor(){
		return mEditor;
	}

	public synchronized SharedPreferences getSharedPreferences(){
		return mSharedPreferences;
	}
}
